package org.cobo.vmmanagement.web;
import java.util.List;

import javax.persistence.TypedQuery;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cobo.vmmanagement.domain.Charge;
import org.cobo.vmmanagement.domain.Project;
import org.cobo.vmmanagement.domain.Resource;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RequestMapping("/resourcehoursdetail/**")
@Controller
public class ResourceHoursDetail {

    @RequestMapping(method = RequestMethod.POST, value = "{id}")
    public void post(@PathVariable Long id, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
    }

    @RequestMapping
    public String index(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
    	
    	Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String userName = auth.getName(); 
        TypedQuery<Resource> resources = Resource.findResourcesByNameEquals(userName);
        Resource resource = resources.getSingleResult();
        
        TypedQuery<Charge> temp = Charge.findChargesByResource(resource);
        List<Charge> charges = temp.getResultList();
        
        modelMap.addAttribute("charges", charges);
    	
        return "resourcehoursdetail/index";
    }
}
